package com.baofu.paycore.service.validate.impl.consume;

import com.baofu.paycore.common.exception.ManagerErrorCode;
import com.baofu.paycore.service.facade.enums.PayCoreErrorCode;
import com.baofu.paycore.common.utils.Constant;
import com.baofu.paycore.service.facade.model.pay.FundDetailInfoDTO;
import com.baofu.paycore.service.facade.model.pay.PayDTO;
import com.baofu.paycore.service.validate.AbstractPayValidate;
import com.system.commons.exception.BizServiceException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 消费服务参数校验
 *
 * User: LZQ
 * Date: 14-11-21
 */
@Slf4j
@Service
public class ConsumeValidate extends AbstractPayValidate<PayDTO>{

    /**
     * 消费服务参数校验
     *
     * @param payDTO    消费参数
     */
    public void paramsValidate(PayDTO payDTO){
        super.paramsValidate(payDTO);

        long payAmt = 0;
        long feeAmt = 0;
        for (FundDetailInfoDTO fundDetailInfoDTO : payDTO.getPayerFundDetails()){
            if (Constant.validateLong(fundDetailInfoDTO.getBizChargeAmount()) >0 &&
                    StringUtils.isEmpty(fundDetailInfoDTO.getBizFeeAccount())){
                log.error("支付收取手续费 请求参数:{}", payDTO);
                throw new BizServiceException(PayCoreErrorCode.BIZ_FEE_ACCOUNT_NOT_NULL);
            }
            payAmt += fundDetailInfoDTO.getPayToolAmount();
            feeAmt += Constant.validateLong(fundDetailInfoDTO.getBizChargeAmount());
        }
        if (payDTO.getAmt() != payAmt){
            log.error("支付总金额:{} 资金单总金额:{}", payDTO.getAmt(), payAmt);
            throw new BizServiceException(ManagerErrorCode.AMT_ERROR);
        }
        if (payDTO.getAmt() <= feeAmt){
            log.error("支付总金额:{} 手续费金额:{}", payDTO.getAmt(), feeAmt);
            throw new BizServiceException(ManagerErrorCode.FEE_AMT_NOT_MORE_PAY_AMT);
        }
    }
}
